/*
 * @Author: fisher luthor-li@qq.com
 * @Date: 2024-03-05 15:58:30
 * @LastEditors: fisher luthor-li@qq.com
 * @LastEditTime: 2024-09-11 12:22:41
 * @FilePath: \allfront\src\utils\fpstream\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  Config,
  PixelStreaming,
} from "@epicgames-ps/lib-pixelstreamingfrontend-ue5.3";
// import { useUemsg } from './utils/ueMsg.js';
import { useUemsg } from "./utils/ueMsgNew.js";
import Gls from "./modules/Gls/index.js";
import Camera from "./modules/Camera/index.js";
import Measure from "./modules/Measure/index.js";
import Analysis from "./modules/Analysis/index.js";
import Marker from "./modules/Marker/index.js";
import Weather from "./modules/Weather/index.js";
import Event from "./modules/Event/index.js";
import Animate from "./modules/Animate/index.js";
// 东莞项目模型生长
import AreabuildGrow from "./modules/AreabuildGrow/index.js";
// 东莞项目建筑分层
import BuildSplit from "./modules/BuildSplit/index.js";
// 东莞地块管理
import BlockManager from "./modules/BlockManager/index.js";
export default class Fpstream {
  videoElementParent;
  wsUrl;
  customConfig;
  stream;
  constructor(dom, url, customConfig) {
    this.videoElementParent = dom;
    this.wsUrl = url;
    this.customConfig = customConfig;
    this.streamStatus = 0;
    this.statusTimer = null;
  }
  connect(onopen, onclose) {
    const initialSettings = {
      AutoPlayVideo: true,
      AutoConnect: true,
      ss: this.wsUrl,
      StartVideoMuted: true,
      WaitForStreamer: true,
      HoveringMouse: false, //显示鼠标，悬停于播放器控件之上不与其交互，要将鼠标移动发送到虚幻引擎需要按住按键，
      MatchViewportRes: true,
    };
    if (this.customConfig) {
      Object.assign(initialSettings, this.customConfig);
    }
    const config = new Config({
      initialSettings: initialSettings,
    });
    // Create a PixelStreaming instance and attach the video element to an existing parent div
    this.stream = new PixelStreaming(config, {
      videoElementParent: this.videoElementParent,
    });
    const { send2Ue, listen2Ue, removeUeListener, clearMsgCb } = useUemsg(
      this.stream
    );
    this.send2Ue = send2Ue;
    this.listen2Ue = listen2Ue;
    this.removeUeListener = removeUeListener;
    this.clearMsgCb = clearMsgCb;
    // 在连接成功后,注册子类
    this.Gls = new Gls(send2Ue, listen2Ue);
    this.Camera = new Camera(send2Ue, listen2Ue);
    this.Measure = new Measure(send2Ue, listen2Ue);
    this.Analysis = new Analysis(send2Ue, listen2Ue);
    this.Marker = new Marker(send2Ue, listen2Ue);
    this.Weather = new Weather(send2Ue, listen2Ue);
    this.Event = new Event(send2Ue, listen2Ue);
    this.Animate = new Animate(send2Ue, listen2Ue);
    // 东莞项目模型生长接口
    this.AreabuildGrow = new AreabuildGrow(send2Ue, listen2Ue);
    this.BuildSplit = new BuildSplit(send2Ue, listen2Ue);
    this.BlockManager = new BlockManager(send2Ue, listen2Ue, removeUeListener);
    // 监听websocket连接状态
    if (!this.statusTimer) {
      this.statusTimer = setInterval(() => {
        if (
          this.streamStatus !== 1 &&
          this.stream.webSocketController.webSocket.readyState == 1
        ) {
          onopen &&
            onopen({
              message: "连接成功！",
              status: 1,
              stream: this.stream,
            });
        }
        if (
          this.streamStatus !== 3 &&
          this.stream.webSocketController.webSocket.readyState == 3
        ) {
          onclose &&
            onclose({
              message: "连接断开！",
              status: 3,
              stream: this.stream,
            });
        }
        this.streamStatus =
          this.stream.webSocketController.webSocket.readyState;
      }, 2000);
    }
  }
  disconnect() {
    this.stream.disconnect();
    clearInterval(this.statusTimer);
    this.statusTimer = null;
    this.streamStatus = 0;
  }
  getStream() {
    return this.stream;
  }
}
